<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>[Politicians Ranking] 회원가입</title>
		<link rel="stylesheet" href="css/common.css">
		<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>
		<script src="js/common.js" type="text/javascript"></script>
		<script type="text/javascript">
		$( document ).ready(
		function(e)
		{
			// 공통 기본 설정
			defaultSetting();
			
			// 아이디 중복체크
			$( '#btn_checkId' ).click( function( e )
										{
											var _id 		= $( '#regist' ).find('input[name="id"]').val();
											if( validateEmail( _id ) == false )
											{
												alert( "이메일형식이 아닙니다." );
												return;
											}
											// 보내기
											$.post( getGatePath(),
											{
												__key__: 'isSameID',
												__returnType__: 'json',
												id: _id
											},
											function( data )
											{
												if( data.result == "SUCCESS" )
												{
													// 사용가능
													setResult( "[이메일 중복체크] 사용가능합니다." );
												}else{
													setResultFailForJSON( "[이메일 중복체크]  이미 존재하는 이메일입니다...", data );
												}
											},
											'json' )
										});
			
			// 닉네임 중복체크
			$( '.btn_checkNickname' ).click( function( e )
										{
											var _nickname 	= $( e.target ).parent().find('input[name="nickname"]').val();
											if( _nickname == undefined || _nickname == "" )
											{
												alert( "닉네임을 입력하세요." );
												return;
											}
											// 보내기
											$.post( getGatePath(),
											{
												__key__: 'isSameNickname',
												__returnType__: 'json',
												nickname: _nickname
											},
											function( data )
											{
												if( data.result == "SUCCESS" )
												{
													// 사용가능
													setResult( "[닉네임 중복체크] 사용가능합니다." );
												}else{
													setResultFailForJSON( "[닉네임 중복체크]  이미 존재하는 닉네임입니다...", data );
												}
											},
											'json' )
										});
			
			
			// [기능] 회원가입 데이터 보내기
			$( "#regist" ).submit( function(event){
				event.preventDefault();
				
				var $form 		= $(this);
				var _id 		= $form.find('input[name="id"]').val();
				var _pass1 		= $form.find('input[name="p1"]').val();
				var _pass2 		= $form.find('input[name="p2"]').val();
				var _nickname 	= $form.find('input[name="nickname"]').val();
				var _sex		= $form.find('select[name="sex"]').val();
				var _birthYear	= $form.find('select[name="birthYear"]').val();
				var _region		= $form.find('select[name="region"]').val();
				
				// 예외 처리
				// 1. 이메일 예외
			    if( validateEmail( _id ) == false )
				{
					alert( "이메일형식이 아닙니다." );
					return;
				}
				// 2. 비밀번호 예외
				if( _pass1 == "" )
				{
					alert( "비밀번호를 입력해 주세요." );
					return;
				}
				if( _pass1 != _pass2 )
				{
					alert( "비밀번호가 동일하지 않습니다." );
					return;
				}
				// 3. 닉네임
				if( _nickname == undefined || _nickname == "" )
				{
					alert( "닉네임을 입력하세요." );
					return;
				}
				// 4. region
				if( _region == undefined )
				{
					alert( "지역을 선택하세요" );
					return;
				}
				
				// 보내기
				$.post( getGatePath(),
				{
					__key__: 'memberRegist',
					__returnType__: 'json',
					id: _id,
					pass: _pass1,
					nickname: _nickname,
					sex: _sex,
					birthYear: _birthYear,
					region: _region
				}, 
				function( data )
				{
					if( data.result == "SUCCESS" )
					{
						setResult( "[가입 성공] ["+data[ 'to' ] +"]로 메일이 발송되었음. 활성키 : "+data[ 'link' ] );
						appendResult( "<a href='"+data[ 'link' ]+"'>클릭시 바로 활성됨</a>" );
					}else{
						setResultFailForJSON( "[가입 실패]", data );
					}
				},
				"json" );
			});
			
			// [회원 가입] SNS 플랫폼
			$( "#registFromSNS" ).submit(function(e) {
                e.preventDefault();
				
				var $form 		= $(this);
				var _platform	= $form.find('select[name="platform"]').val();
				var _token 		= $form.find('input[name="token"]').val();
				var _nickname 	= $form.find('input[name="nickname"]').val();
				var _sex		= $form.find('select[name="sex"]').val();
				var _birthYear	= $form.find('select[name="birthYear"]').val();
				var _region		= $form.find('select[name="region"]').val();
				
				// 예외 처리
				// 3. 닉네임
				if( _nickname == undefined || _nickname == "" )
				{
					alert( "닉네임을 입력하세요." );
					return;
				}
				// 4. region
				if( _region == undefined )
				{
					alert( "지역을 선택하세요" );
					return;
				}
				
				// 보내기
				$.post( getGatePath(),
				{
					__key__: 'memberRegist',
					__returnType__: 'json',
					platform: _platform,
					token: _token,
					nickname: _nickname,
					sex: _sex,
					birthYear: _birthYear,
					region: _region
				}, 
				function( data )
				{
					if( data.result == "SUCCESS" )
					{
						setResult( "[가입 성공] 신규 발급 member seq :"+data.seq );
					}else{
						setResultFailForJSON( "[가입 실패]", data );
					}
				},
				"json" );
            });
		});
		</script>
	</head>
	<body>
		<div id="result">
	    </div>
		<h1>1. 일반 회원 가입</h1>
	    <hr/>
	    <form id="regist" action="#">
	    	<table>
	        <tr>
		        <td>Email : </td>
	            <td><input type="email" name="id" placeholder="Email" /><input type='button' id='btn_checkId' value='중복체크'></td>
	        </tr>
	        <tr>
	        	<td>Pass : </td>
	            <td><input type="password" name="p1" placeholder="Password" /></td>
			</tr>
	        <tr>
	        	<td>Pass Again : </td>
	            <td><input type="password" name="p2" placeholder="Password Again" /></td>
			</tr>
	        <tr>
	        	<td>Nickname : </td>
	            <td><input type="text" name="nickname" placeholder="Nickname" /><input type='button' class='btn_checkNickname' value='중복체크'></td>
			</tr>
	        <tr>
		        <td>Sex : </td>
	            <td><select name="sex" class='selectSex'></select></td>
	        </tr>
	        <tr>
	        	<td>Birth Year : </td>
	            <td><select name="birthYear" class='selectBirth'></select></td>
	        </tr>
            <tr>
		        <td>출신지역 : </td>
	            <td><select name="region" class='selectRegion'></select></td>
	        </tr>
	        <tr>
	        	<td>AUTHOR : </td>
	            <td>일반 회원</td>
	        </tr>
	        </table>
	        <input type="submit" value="Regist" />
	  	</form>
    	<hr/>
        <h1>2. SNS 회원 가입</h1>
        <form id="registFromSNS" action="#">
	    	<table>
	        <tr>
		        <td>플랫폼 : </td>
	            <td><select name="platform" class='selectRegistPlatform'></select></td>
	        </tr>
	        <tr>
	        	<td>Token : </td>
	            <td><input name="token" type="text" /></td>
			</tr>
	        <tr>
	        	<td>Nickname : </td>
	            <td><input type="text" name="nickname" placeholder="Nickname" /><input type='button' class='btn_checkNickname' value='중복체크'></td>
			</tr>
	        <tr>
		        <td>Sex : </td>
	            <td><select name="sex" class='selectSex'></select></td>
	        </tr>
	        <tr>
	        	<td>Birth Year : </td>
	            <td><select name="birthYear" class='selectBirth'></select></td>
	        </tr>
            <tr>
		        <td>출신지역 : </td>
	            <td><select name="region" class='selectRegion'></select></td>
	        </tr>
	        <tr>
	        	<td>AUTHOR : </td>
	            <td>일반 회원</td>
	        </tr>
	        </table>
	        <input type="submit" value="Regist" />
	  	</form>
	</body>
</html>